package com.lvmama.pangolin.proxyip.biz;


import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.lvmama.pangolin.entity.IP;
import com.lvmama.pangolin.proxyip.inter.TokenProxyIPProcesser;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by NUCTONY on 2016/4/3.
 */
public class TokenCollectProxyIP extends DefaultCollectProxyIP {

    private static final Logger logger = Logger.getLogger(TokenCollectProxyIP.class);


    public TokenCollectProxyIP(String url, String urlSuffix, String userAgent, String referrer, String host, int startPage, int endPage, int requestNum) {
        super(url, urlSuffix, userAgent, referrer, host, startPage, endPage, requestNum);
    }

    public TokenCollectProxyIP(String url, String urlSuffix, String userAgent, String referrer, String host, int startPage, int endPage) {
        this(url, urlSuffix, userAgent, referrer, host, startPage, endPage, defaultRequestNum);

    }

    public TokenCollectProxyIP(String url, String urlSuffix, String userAgent, String referrer, int startPage, int endPage) {
        this(url, urlSuffix, userAgent, referrer, referrer.substring(7), startPage, endPage);

    }

    public TokenCollectProxyIP(String url, String urlSuffix, String userAgent, int startPage, int endPage) {
        this(url, urlSuffix, userAgent, url.substring(0, url.indexOf("/", 7)), startPage, endPage);

    }

    public TokenCollectProxyIP(String url, String urlSuffix, int startPage, int endPage) {
        this(url, urlSuffix, defaultUserAgent, startPage, endPage);

    }

    public TokenCollectProxyIP(String url, String urlSuffix, int endPage) {
        this(url, urlSuffix, defaultUserAgent, 1, endPage);

    }

    public TokenCollectProxyIP(String url, int startPage, int endPage) {
        this(url, "", defaultUserAgent, startPage, endPage);

    }

    public TokenCollectProxyIP(String url, int endPage) {
        this(url, "", defaultUserAgent, 1, endPage);

    }

    public TokenCollectProxyIP(String url, String urlSuffix) {
        this(url, urlSuffix, 1, 1);
    }

    public TokenCollectProxyIP(String url) {
        this(url, "");
    }

    public List<IP> doRequest(TokenProxyIPProcesser processer) {
        Document doc;
        List<IP> proxyIPVOs = new ArrayList<IP>();
        for (int i = getStartPage(); i <= getEndPage(); i++) {
            String reqUrl = getUrl() + i + getUrlSuffix();
            logger.info("获取数据：" + reqUrl);
            boolean flag = false;
            int errorNum = 1;
            while (!flag) {
                try {
                    doc = Jsoup.connect(reqUrl).header("host", getHost()).referrer(getReferrer()).userAgent(getUserAgent()).get();
                    proxyIPVOs.addAll(processer.parseDoc(doc, processer.getToken(doc)));
                    Thread.sleep(1000);
                    flag = true;
                } catch (Exception e) {
                    logger.error(e.getMessage());
                    if (errorNum >= getRequestNum()) {
                        logger.error("已达到最大重试次数：" + getRequestNum() + "");
                        flag = true;
                    }
                    errorNum++;
                }
            }
        }
        return proxyIPVOs;
    }


}
